<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Logic\Article as ArticleLogic;
use App\Logic\ArticleCategory as ArticleCategoryLogic;
use App\Logic\Tags as TagsLogic;
use App\Http\Requests\Article as ArticleRequest;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{
    private $articleLogic;
    private $articleCategoryLogic;
    private $tagsLogic;
    private $articleRequest;

    public function __construct()
    {
        $this->articleLogic = new ArticleLogic();
        $this->articleCategoryLogic = new ArticleCategoryLogic();
        $this->tagsLogic = new TagsLogic();
        $this->articleRequest = new ArticleRequest();
    }

    public function index()
    {
        $category = $this->articleCategoryLogic->select();
        return view('article.index', ['category' => $category]);
    }

    public function list(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $start = ($page - 1) * $limit;
        $where['article_title'] = $request->input('title', '');
        $where['cate_id'] = $request->input('cid', '');
        $where['is_recom'] = $request->input('is_recom', 0);
        $result = [
            'code' => 0,
            'msg' => '获取数据成功！',
            'count' => $this->articleLogic->count($where),
            'data' => $this->articleLogic->list((int)$start, (int)$limit, $where),
        ];
        return response()->json($result);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'新增失败！'];

            $data['cate_id'] = $request->post('cid',0);
            $data['article_title'] = $request->post('title','');
            $data['article_img'] = $request->post('img','');
            $data['read_num'] = $request->post('read',0);
            $data['article_status']= $request->post('status',0);
            $data['is_recom']= $request->post('recom',1);
            $data['article_tags'] = $request->post('tags','');
            $data['article_content'] = $request->post('content','');

            $validator = Validator::make($data, $this->articleRequest->rules(), $this->articleRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if ($this->articleLogic->add($data)){
                $result['status'] = true;
                $result['msg'] = '新增成功！';
                return response()->json($result);
            }

            return response()->json($result);

        } else {
            $tags = $this->tagsLogic->select();
            $category = $this->articleCategoryLogic->select();
            return view('article.add', ['category' => $category, 'tags' => $tags]);
        }
    }

    public function edit(Request $request, $id = 0)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'修改失败！'];
            $id= $request->post('id',0);
            $data['article_title'] = $request->post('title','');
            $data['cate_id'] = $request->post('cid',0);
            $data['article_img'] = $request->post('img','');
            $data['read_num'] = $request->post('read',0);
            $data['article_status']= $request->post('status',0);
            $data['is_recom']= $request->post('recom',0);
            $data['article_tags'] = $request->post('tags','');
            $data['article_content'] = $request->post('content','');

            if(($id < 1) || !($this->articleLogic->has((int)$id))){
                $result['msg'] = '请勿非法提交！';
                return response()->json($result);
            }

            $validator = Validator::make($data, $this->articleRequest->rules(), $this->articleRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if ($this->articleLogic->update((int)$id, $data)){
                $result['status'] = true;
                $result['msg'] = '修改成功！';
                return response()->json($result);
            }

            return response()->json($result);

        } else {
            if ($id < 1) abort(404);
            $data = $this->articleLogic->find((int)$id)->toArray();
            if (empty($data)) abort(404);
            $data['article_tags'] = [];
            foreach ($data['tags'] as $v){
                $data['article_tags'][]=$v['id'];
            }
            $tags = $this->tagsLogic->select();
            $category = $this->articleCategoryLogic->select();
            return view('article.edit', ['data' => $data, 'category' => $category, 'tags' => $tags]);
        }
    }

    public function delete(Request $request, $id = 0)
    {
        $result = ['status'=>false, 'msg'=>'删除失败！'];
        if(($id < 1) || !($this->articleLogic->has((int)$id))){
            $result['msg'] = '请勿非法提交！';
            return response()->json($result);
        }

        if ($this->articleLogic->delete((int)$id)){
            $result['status'] = true;
            $result['msg'] = '删除成功！';
            return response()->json($result);
        }
        return response()->json($result);
    }

    public function status(Request $request, $id = 0)
    {
        $data = ['status' => false, 'msg' => '状态设置失败！'];
        if ($id >= 1){
            if ($this->articleLogic->status((int)$id)){
                $data['status'] = true;
                $data['msg'] = '状态设置成功！';
                return response()->json($data);
            }
        }
        return response()->json($data);
    }
}
